/**    
 * @{#} BuweiActivity.java Create on 2014-6-9 下午8:50:36    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.skinapp.R;

import com.evan.common.utils.CacheCommon;
import com.evan.common.widget.PinnedSectionListView;
import com.smiier.skin.adapter.BuweiAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.PartTask;
import com.smiier.skin.net.PartTask.PartRequest;
import com.smiier.skin.net.PartTask.PartResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.vo.SectionRow;
import com.umeng.analytics.MobclickAgent;

public class BuweiActivity extends BasicActivity implements OnItemClickListener {

	PinnedSectionListView list_buwei;
	private HashMap<String, String> buweiMap = new HashMap<String, String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_buwei);

		init();
		setNavLeftBtn("取消");
		setNavLeftBtnNoDrawable();
		setNavRightBtn("确认");
		setNavTitle("选择部位");

		list_buwei = (PinnedSectionListView) findViewById(R.id.list_buwei);
		ArrayList<CacheCommon> caches = dbUtil.queryCache(getClass()
				.getSimpleName());
		if (caches.size() > 0) {
			String data = caches.get(0).data;
			try {
				o(new JSONArray(data));
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			PartRequest req = new PartRequest();
			PartTask task = new PartTask();
			task.setRequest(req);
			task.addListener(new NetTaskListener<PartTask.PartRequest, PartTask.PartResponse>() {

				@Override
				public void onException(
						INetTask<PartRequest, PartResponse> task, Exception e) {

				}

				@Override
				public void onComplete(
						INetTask<PartRequest, PartResponse> task,
						PartResponse response) {
					try {
						String o = JsonUtil.convert(response);
						JSONArray array = new JSONArray(o);
						dbUtil.saveOrUpdate(this.getClass().getSimpleName(), o);
						o(array);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			add(task);
		}
	}

	void o(JSONArray array) {
		try {
			ArrayList<SectionRow> rows = new ArrayList<SectionRow>();
			String[] buwei_head = new String[array.length()];
			String[][] buwei_content = new String[array.length()][];
			for (int i = 0; i < array.length(); i++) {
				JSONObject oo = array.getJSONObject(i);
				buwei_head[i] = oo.getString("Key");
				JSONArray values = oo.getJSONArray("Value");
				String[] v = new String[values.length()];
				for (int j = 0; j < values.length(); j++) {
					v[j] = values.getString(j);
				}
				buwei_content[i] = v;
			}
			int sectionPosition = 0, listPosition = 0;
			for (int i = 0; i < buwei_head.length; i++) {
				SectionRow section = new SectionRow(SectionRow.SECTION,
						buwei_head[i]);
				section.sectionPosition = sectionPosition;
				section.listPosition = listPosition++;
				rows.add(section);

				for (String text : buwei_content[i]) {
					SectionRow item = new SectionRow(SectionRow.ITEM, text);
					item.sectionPosition = sectionPosition;
					item.listPosition = listPosition++;
					rows.add(item);
				}
				sectionPosition++;
			}
			BuweiAdapter adapter = new BuweiAdapter(this, rows);
			list_buwei.setAdapter(adapter);
			list_buwei.setOnItemClickListener(BuweiActivity.this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void onClick(View view) {
		int id = view.getId();
		if (id == R.id.btn_left) {
			finish();
		} else if (id == R.id.text_right) {
			if (buweiMap.size() > 0) {
				// if(buweiMap.size() < 4) {
				Intent intent = new Intent();
				StringBuilder sb = new StringBuilder();
				for (String value : buweiMap.values()) {
					sb.append(value).append(",");
				}
				if (sb.length() > 0 && sb.lastIndexOf(",") != -1) {
					intent.putExtra(Constant.IDENTITY_KEY,
							sb.substring(0, sb.lastIndexOf(",")));
				}
				setResult(Constant.ACTIVITY_CHOOSE_BUWEI, intent);
				finish();
				// } else {
				// CommonUtility.tip(activity, "部位选择个数不能超过3个");
				// }
			} else {
				CommonUtility.tip(this, "请选择部位");
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		CheckBox checkBox = (CheckBox) view.getTag();
		if (!CommonUtility.isNull(checkBox)) {
			SectionRow row = (SectionRow) checkBox.getTag();
			boolean isCheck = checkBox.isChecked();
			checkBox.setChecked(!isCheck);
			if (!isCheck) {
				buweiMap.put(row.text, row.text);
			} else {
				buweiMap.remove(row.text);
			}

			if (buweiMap.size() > 3) {
				CommonUtility.tip(this, "部位选择个数不能超过3个");
				checkBox.setChecked(false);
				buweiMap.remove(row.text);
			}
		}
	}
	
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
